﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class TestString
    {
        static void Main(string[] args)
        {
            string s1 = "张三,李四,王五,赵六";

            /*
            Console.Write("请输入要查找的人名：");
            string name = Console.ReadLine();

            //Contains方法用于判断字符串中是否包含指定子字符串，返回true或false
            if (s1.Contains(name))
            {
                Console.WriteLine("字符串中包含{0}！", name);
            }
            else
            {
                Console.WriteLine("查无此人！");
            }
            //IndexOf方法与Contains方法的作用相似，都是去字符串中查找有没有指定子字符串
            //只不过Contains方法返回布尔值，而IndexOf方法返回下标(位置)，如果没有返回-1
            if (s1.IndexOf(name) != -1)
            {
                Console.WriteLine("字符串中包含{0}！", name);
            }
            else
            {
                Console.WriteLine("查无此人！");
            }

            //LastIndexOf方法与IndexOf方法的区别是从后往前找
            string s2 = "fjwofwjiwjo";
            Console.WriteLine(s2.LastIndexOf("j"));

            Console.Write("请输入要查找的字符：");
            string str = Console.ReadLine();
            Console.WriteLine("字符串中有3个j，位置分别是：1、6、9");
            */


            /*
            Console.Write("学号：");
            int stuNo = int.Parse(Console.ReadLine());
            Console.Write("姓名：");
            string stuName = Console.ReadLine();
            Console.Write("年龄：");
            int stuAge = int.Parse(Console.ReadLine());
            Console.Write("性别：");
            char stuSex = char.Parse(Console.ReadLine());

            //string sql = "insert into stuInfo values(1001,'张三',18,'男')";

            //通过拼接字符串的方式，构建sql语句
            //string sql = "insert into stuInfo values("+stuNo+",'"+ stuName + "',"+stuAge+",'"+stuSex+"')";

            string sql = "insert into stuInfo values({0},'{1}',{2},'{3}')";
            //通过Format方法，构建sql语句
            //Format方法可以对带有占位符的字符串进行格式化，将具体的值放入对应的占位符
            sql = String.Format(sql, stuNo, stuName, stuAge, stuSex);

            Console.WriteLine(sql);
            */

            //找到王五的位置
            int index = s1.IndexOf("王五");
            //将张博文插入到王五前面
            s1 = s1.Insert(index, "张博文,");
            Console.WriteLine(s1);

            //Remove方法可以删除字符串中指定位置的子字符串
            //s1 = s1.Remove(6);
            s1 = s1.Remove(3,3);
            Console.WriteLine(s1);

            //Replace方法可以将字符串中某个子字符串替换为新字符串
            s1 = s1.Replace("张博文", "奥特曼");
            Console.WriteLine(s1);

            //将字符串按分隔符切割成一个子字符串数组
            string[] names = s1.Split(',');

            for (int i = 0; i < names.Length; i++)
            {
                Console.WriteLine(i+1 + "、" + names[i]);
            }


        }
    }
}
